<?php

namespace App\Http\Controllers\Product;

use App\Models\Product\Project;
use App\Models\Product\ProjectProduct;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Helpers\Code;

class ProjectController extends Controller
{
    /**
     * @author lcy
     */
    public function __construct()
    {
        $this->_validators();
        $this->model = new Project();
    }

    /**
     * 添加项目
     * @param Request $request
     * @return array
     */
    public function addProject(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addProject($data);
            $this->checked($model, [
                'null' => 'product.project.dependence_non_exist',
                Code::INSIDE_ERROR => 'product.project.organization_non_exist',
                Code::INSIDE_ERROR_1 => 'product.project.leader_non_exist',
            ]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑项目
     * @param Request $request $id
     * @return array
     */
    public function editProject(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['code', 'data_scope', 'created_by']));
            unset($this->rules['code']);
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editProject($data, $id);
            $this->checked($model, [
                'null' => 'common.get_failure',
                Code::INSIDE_ERROR => 'product.project.dependence_non_exist',
                Code::INSIDE_ERROR_1 => 'product.project.organization_non_exist',
                Code::INSIDE_ERROR_2 => 'product.project.leader_non_exist',
            ]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定项目
     * @param $id
     * @return array
     */
    public function getProject($id)
    {
        try {
            $model = $this->model->getProject($id);
            $this->checked($model, ['null' => 'common.get_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取项目列表
     * @param Request $request
     * @return array
     */
    public function getProjectList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);
            $limit = (int)$request->input('limit');
            $sorter = $this->handleSort($request, $this->model->sorters);
            $defaultSorter = $request->input('sorter') ?? 'desc';
            return preprocess($this->model->getProjectList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定项目包含产品
     * @param $id
     * @return array
     */
    public function getProjectProductAll($id){
        try {
            $this->model = new ProjectProduct();
            $model = $this->model->getProjectProductAll($id);
            $this->checked($model, ['null' => 'common.get_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'title' => 'bail|required|max:200',
            'code' => 'bail|required|max:200|unique:projects',
            'description' => 'max:200',
            'customer' => 'nullable|numeric',
            'department' => 'nullable|numeric',
            'doctor' => 'nullable|numeric',
            'organization' => 'max:200',
            'type' => 'max:200',
            'leader' => 'nullable|numeric',
            'salesman' => 'nullable|numeric',
            'money' => 'nullable|numeric',
            'state' => 'bail|required|in:0,1',
        ];

        $this->messages = [
            'title.required' => trans('product.project.title_required'),
            'title.max' => trans('product.project.title_max'),
            'code.required' => trans('product.project.code_required'),
            'code.max' => trans('product.project.code_max'),
            'code.unique' => trans('product.project.code_unique'),
            'description.max' => trans('product.project.description_max'),
            'customer.numeric' => trans('product.project.customer_numeric'),
            'department.numeric' => trans('product.project.department_numeric'),
            'doctor.numeric' => trans('product.project.doctor_numeric'),
            'organization.max' => trans('product.project.organization_max'),
            'type.max' => trans('product.project.type_max'),
            'leader.numeric' => trans('product.project.leader_numeric'),
            'salesman.numeric' => trans('product.project.salesman_numeric'),
            'money.numeric' => trans('product.project.money_numeric'),
            'state.required' => trans('product.project.state_required'),
            'state.in' => trans('product.project.state_in'),
        ];
    }
}
